package com.suxin.splitter.spring;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Tang
 * @classname SingleParamSplitInvoker
 * @description [  ]
 * @date 2022/11/9 17:24
 */
public class SingleParamSplitInvoker extends AbstractSplitInvoker{

    public SingleParamSplitInvoker(SplitService splitService, Method method, int sizePrePartition) {
        super(splitService, method, sizePrePartition);
    }

    @Override
    public Object invoke(Object target, Object[] args) {
        return this.getSplitService().split((params) -> {
            return invokeMethod(target, params);
        }, args[0],getSizePrePartition());
    }

    private Object invokeMethod(Object target, Object param) {
        Object[] paramToUse = new Object[]{param};
        try {
            return getMethod().invoke(target, paramToUse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
